/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.application.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.application.ApplicationService;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.jpa.Application;
import org.toools.gwt.wmc.jpa.CompanyPart;

/**
 *
 * @author manuel
 */
public class ApplicationListRenderer extends BasicListRenderer{

    private Application app = null;

    BasicTogglePanel companyparts = new BasicTogglePanel("set companyparts", 1, true) {
        public void onActive() {
            clear();
            addChild(new CompanyPartsRelation((Application) getTag().getAttachedBusinessObject()));
        }
    };

    public ApplicationListRenderer(TagVO tag, BasicControl bc) {
		super(tag, bc);

        this.app = (Application)tag.getAttachedBusinessObject();

//        questionable if this should be done here
//        addButton(companyparts);
	}

    @Override
    public void setTag(TagVO tag) {
		Application ap = (Application) tag.getAttachedBusinessObject();
		setTitle(ap.getContent());
        if(ap.getCompanyParts() != null) {
            if(ap.getCompanyParts().size() == 0) {
                this.setSubTitle("no companyparts");
            } else if(ap.getCompanyParts().size() == 1) {
                this.setSubTitle("one companypart");
            } else if(ap.getCompanyParts().size() > 1) {
                this.setSubTitle("" + ap.getCompanyParts().size() + " companyparts");
            }
        }
	}

    private class CompanyPartsRelation extends RelationControl {
        Application application;
        public CompanyPartsRelation(Application a) {
            super(false, true, true, false);
            this.application = a;
            CompanyPartService.Util.getInstance().getCompanyParts(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }

        public TagVO getTagVO(Object bp) {
            CompanyPart cp = (CompanyPart) bp;
            TagVO result = new TagVO("" + cp.getId(), cp.getName(), 0, 0);
            result.setAttachedBusinessObject(cp);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<CompanyPart>)result).toArray()));
                if((app.getCompanyParts() != null) && (app.getCompanyParts().size() > 0)) {
                    String[] active = new String[app.getCompanyParts().size()];
                    int j = 0;
                    for(Iterator i = app.getCompanyParts().iterator(); i.hasNext();) {
                        CompanyPart cp = (CompanyPart) i.next();
                        active[j++] = "" + cp.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                List<CompanyPart> cps = new ArrayList<CompanyPart>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    cps.add((CompanyPart)tag.getAttachedBusinessObject());
                }
                application.setCompanyParts(cps);
                ApplicationService.Util.getInstance().update(application, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }

}
